/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.RedisUtils;
import io.renren.modules.project.dao.*;
import io.renren.modules.project.entity.BackendEntity;
import io.renren.modules.project.entity.DatasourceEntity;
import io.renren.modules.project.entity.FrontendEntity;
import io.renren.modules.project.entity.ModuleEntity;
import io.renren.modules.project.service.BackendService;
import io.renren.modules.project.service.DatasourceService;
import io.renren.modules.systemconfig.entity.ProjectBaseEntity;
import io.renren.modules.systemconfig.service.ProjectBaseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service("projectBaseService")
public class ProjectBaseServiceImpl extends ServiceImpl<ProjectBaseDao, ProjectBaseEntity> implements ProjectBaseService {
	@Autowired
	private ModuleDao moduleMapper;
	@Autowired
	private FrontendDao frontMapper;
	@Autowired
	private BackendService backendService;
	@Autowired
	private DatasourceDao datasourceMapper;

	@Autowired
	private RedisUtils redisUtils;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String key = (String)params.get("key");

		IPage<ProjectBaseEntity> page = this.page(
			new Query<ProjectBaseEntity>().getPage(params),
			new QueryWrapper<ProjectBaseEntity>()
				.like(StringUtils.isNotBlank(key),"name", key).or().like(StringUtils.isNotBlank(key),"ip_addr", key)
		);

		return new PageUtils(page);
	}

	@Override
	public PageUtils treeList(Map<String, Object> params) {
		String key = (String)params.get("key");
		IPage<ProjectBaseEntity> page = this.page(
				new Query<ProjectBaseEntity>().getPage(params),
				new QueryWrapper<ProjectBaseEntity>()
						.like(StringUtils.isNotBlank(key),"name", key).or().like(StringUtils.isNotBlank(key),"ip_addr", key)
		);
		for(ProjectBaseEntity pbe:page.getRecords()){
			List<FrontendEntity> fronts = frontMapper.selectList(new QueryWrapper<FrontendEntity>().eq("project_id",pbe.getId()));
			fronts.forEach(s -> {
				System.out.println("front-"+s.getId()+":"+redisUtils.get("front-"+s.getId()));
				s.setRuntimeStatus(redisUtils.get("front-"+s.getId()));
			});
			pbe.setFrontends(fronts);
			pbe.setBackend(backendService.getBackendTreeByProjectId(pbe.getId()));
			List<DatasourceEntity> datasources = datasourceMapper.selectListByProjectId(pbe.getId());
			datasources.forEach(s -> {
				System.out.println("datasource-"+s.getId()+":"+redisUtils.get("datasource-"+s.getId()));
				s.setRuntimeStatus(redisUtils.get("datasource-"+s.getId()));
			});
			pbe.setDatasources(datasources);
		}
		return new PageUtils(page);
	}


	@Override
	public ProjectBaseEntity queryByKey(String key) {
		return baseMapper.queryByKey(key);
	}

	@Override
	@Transactional
	public void saveEntity(ProjectBaseEntity entity) {
		this.baseMapper.insert(entity);
	}

	@Override
	@Transactional
	public void update(ProjectBaseEntity user) {
		this.updateById(user);
	}

	@Override
	public void deleteBatch(Long[] userId) {
		this.removeByIds(Arrays.asList(userId));
	}



}